package org.dsc.amafinder2chance;

import org.dsc.amafinder2chance.model.AmfQR;
import org.dsc.sdk.DSC;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

@SuppressLint("SetJavaScriptEnabled")
public class AWebView extends Activity {
   private WebView mWebView;
   private String url = null;
   private String paisBusqueda = "es";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.awebview);

      mWebView = (WebView) findViewById(R.id.awebview);
      mWebView.setWebViewClient(new AWebViewClient());
      android.webkit.WebSettings webSettings = mWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webSettings.setAllowFileAccess(false);
      setupWebsettings(webSettings);
      if (this.getIntent().getExtras() != null) {
          if (this.getIntent().getExtras().getString("URL") != null) {
             url = this.getIntent().getExtras().getString("URL");
          }
          if (this.getIntent().getExtras().getString("PAISBUSQUEDA") != null) {
              paisBusqueda = this.getIntent().getExtras().getString("PAISBUSQUEDA");
           }
       }      
        mWebView.loadUrl(url);
      
      DSC.A(this).create();
   }

   @TargetApi(Build.VERSION_CODES.HONEYCOMB)
   public void setupWebsettings(android.webkit.WebSettings webSettings) {
      // Make sure we're running on Honeycomb or higher to use ActionBar APIs
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
         webSettings.setAllowContentAccess(false);
      }
   }

   @Override
   protected void onResume() {
      super.onResume();     
      DSC.A(this).resume();
   }

   @Override
   protected void onRestart() {
      super.onRestart();
      DSC.A(this).restart();
   }

   private class AWebViewClient extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         if (!url.contains("/offer-listing/")) {
            // ENVIAR A COMPARAR PRECIOS
            DSC.A().log("Enviamos a comprar " + url);
            compararPrecios(url,paisBusqueda);
         } else {
            DSC.A().log("shouldOverrideUrlLoading " + url);
            seguirEnVentana(url,paisBusqueda);
         }
         return true;
      }
   }

   private boolean compararPrecios(String url, String pais) {
      String asin = AmfQR.asin(url);
     /* if (!asin.equalsIgnoreCase("recargar")) {
    	  Uri uri = AmfQR.amazonBuyUri(pais, asin);
          startActivity(new Intent(Intent.ACTION_VIEW, uri));
         
      } else {
         seguirEnVentana(url,pais);
      }*/
    if (!asin.equalsIgnoreCase("recargar")) {
    	  Intent i = new Intent(this,AComparePrices.class);
    	  i.putExtra("ASIN", asin);
    	  i.putExtra("PAISBUSQUEDA",pais);
    	  startActivity(i);        
      } else {
         seguirEnVentana(url,pais);
      }
      return true;
   }

   private boolean seguirEnVentana(String url, String pais) {
      Intent i = new Intent(this, AWebView.class);
      i.putExtra("URL", url);
      i.putExtra("PAISBUSQUEDA", pais);
      startActivity(i);
      return true;
   }

   @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
      // para las demas cosas, se reenvia el evento al listener habitual
      return super.onKeyDown(keyCode, event);
   }

   /*
    * @ini-menu-action-bar
    */
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu items for use in the action bar
      getMenuInflater().inflate(R.menu.main_activity_actions, menu);
      menu.findItem(R.id.action_search).setIntent(new Intent(this, ASearch.class));
      menu.findItem(R.id.action_info).setIntent(new Intent(this, AInfo.class));
      menu.findItem(R.id.action_favorites).setIntent(new Intent(this, AFavorites.class));
      return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.action_search:
      case R.id.action_info:      
         finish();
         startActivity(item.getIntent());
         break;
      case R.id.action_favorites:
    	  startActivity(item.getIntent());
          break;
      default:
         return super.onOptionsItemSelected(item);
      }
      return true;
   }
   /*
    * @end-menu-action-bar
    */
}
